Python

Text to Speech in Python | Google Text to Speech API

Text to Speech in Python | Google Text to Speech API

How to Convert Text to Speech in Python?

Python comes with a lot of handy and easily accessible libraries. Here we will discuss one more interesting library that can easily read aloud any digital text. It can read aloud any kind of text file including word document, online webpages, and any page document. 

In python, there are many APIs available to convert text to speech. One such APIs is the GTTS that stands for Google Text to Speech API. This API is a very easy to use tool Which converts the entered text, into audio. We can also

Save this as an mp3 file. This API supports many languages like English, Hindi, Tamil, French, and many more. Now let's start the discussion more about it

in brief. In this article topics that we will cover are listed below:

  • Module installation and importing
  • Parameters
  • Exceptions that it raises
  • Languages
  • Examples

The syntax for module Installation:

pip install pyttsx3

The syntax for module Importing:

import pyttsx3

Parameters:

  • text(string): The text to be read aloud.
  • TLD(string): Stands for Top-level domain for Google Translate Host, this is useful when default google.com is blocked in within some network but google.cn(local or different host is available.)
  • lang(string, optional): This is used to define the language in which text will be read aloud, by default it is “en”.
  • slow(bool, optional): this can be used if you want to read the text more slowly, by default it is False
  • lang_check(bool, optional): This is used to check the existing of the language, if it is set to TRUE, and the language does not exist then it will raise value error, but

if set to FALSE then it stops the request to validate the language.

  • tokenizer_fun(callable): it is a function which is callable, it takes a string and returns a list of string(tokens).

Exceptions that it raises:

  • ValueError: when lang_check is True and language is not supported.
  • RuntimeError: when lang_check is True but there is an error in loading the language dictionary.
  • AssertionError: when there is nothing to read or text is

Languages:

It takes TLD(string) as parameter i.e top-level domain for GTTS to fetch language from .com by default but if it is blocked within the network then it will look for google.cn.

Its return type is dict(dictionary).

Now let's discuss the code of how we can use this module to convert the digital text in speech.

Example code:

When you will run your program, you will listen to the voice which you have typed inside .say function'''
#code:
# importing pyttsx module
import pyttsx3

# initializing the module
engine = pyttsx3.init()

# .say() function is used to speak the text you have written inside this.
# inside the function
engine.say("Anything that you write here will be spoken by your computer during the running of the program")

# this is used to process and run the program commands
engine.runAndWait()

 Output:

This will convert the text written in the function .say().

Hope you like this article, we will learn more about this

Interesting python module in upcoming articles and will create some application with this module. So stay tuned

with us to keep on practicing and learning.

Mousmi Kumari

Computer Science Engineer/Machine Learner/Python Developer